package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.gold.GoldDto;
import com.java110.dto.integral.IntegralDto;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.po.gold.GoldPo;
import com.java110.po.integral.IntegralPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("queryCommunityIntegralBmoImpl")
public class QueryCommunityIntegralBmoImpl implements IMallCommonApiBmo {

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;

    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "未包含小区");
        Assert.hasKeyAndValue(reqJson, "communityName", "未包含小区名称");
        Assert.hasKeyAndValue(reqJson, "storeName", "未包含物业名称");
        Assert.hasKeyAndValue(reqJson, "storeId", "未包含物业ID");
    }

    @Override
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {

        IntegralDto integralDto = new IntegralDto();
        integralDto.setObjId(reqJson.getString("communityId"));
        integralDto.setObjType(IntegralDto.OBJ_TYPE_STORE);
        List<IntegralDto> integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);
        if (ListUtil.isNull(integralDtos)) {
            IntegralPo integralPo = new IntegralPo();
            integralPo.setIntegralName(reqJson.getString("communityName"));
            integralPo.setAmount("0");
            integralPo.setObjId(reqJson.getString("communityId"));
            integralPo.setLink("1");
            integralPo.setIntegralId(GenerateCodeFactory.getGeneratorId("11"));
            integralPo.setObjType(IntegralDto.OBJ_TYPE_STORE);
            integralV1InnerServiceSMOImpl.saveIntegral(integralPo);
            integralDto.setObjId(reqJson.getString("communityId"));
            integralDto.setObjType(IntegralDto.OBJ_TYPE_STORE);
            integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);
        }

        context.setResponseEntity(ResultVo.createResponseEntity(integralDtos));
    }
}
